<?php
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011 Alexandre Vaillancourt                                  //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
////////////////////////////////////////////////////////////////////////////////

/**
 * Logging facility
 * 
 * @author Alexandre Vaillancourt <alexandre.vaillancourt@gmail.com>
 * @license http://code.google.com/p/rappelz-hunbot/wiki/License MIT License
 * @copyright Copyright (c) 2011 Alexandre Vaillancourt
 * @since v.0.1.0
 * @package rappelz-hunbot
 */

 /**
  * Required to interface with DB.
  */
require_once('include.db_objects.php');

/**
 * class Log
 * Singleton class used to log text in the database.
 * 
 * @package rappelz-hunbot
 */
class Log {
	
	// levels
	/**
	 * Used to retreive data. Should not be used to log anything.
	 */
	const LEVEL_ALL = 0;
	/**
	 * Messages only used for debugging.
	 */
	const LEVEL_DEBUG = 10000;
	/**
	 * Message records an error, but that is not fatal.
	 */
	const LEVEL_ERROR = 40000;
	/**
	 * Message records an error from which it is not possible to recover.
	 */
	const LEVEL_FATAL = 50000;
	/**
	 * Message records an information.
	 */
	const LEVEL_INFO = 20000;
	/**
	 * Used to retreive data. Should not be used to log anything.
	 */
	const LEVEL_OFF = PHP_INT_MAX;
	/**
	 * Message records a warning.
	 */
	const LEVEL_WARN = 30000;
	
	private static $_instance = null;
	
	private $_logentry = null;
	
	private function __construct() {
		$this->_logentry = new logentry();
	}
	
	/**
	 * function get_instance
	 * Gets the instance of the class
	 * @return Log instance of the singleton
	 */
	public static function get_instance() {
		if(Log::$_instance == null) {
			Log::$_instance = new Log();
		}
		
		return Log::$_instance;
	}
	
	/**
	 * function log
	 * Adds a log entry in the database
	 * @param int $level level of the entry
	 * @param string $text text of the entry
	 */
	public function log($level, $text) {
		$this->_logentry->level = $level;
		$this->_logentry->time = time();
		$this->_logentry->entry = $text;
		
		$this->_logentry->SaveNew();
	}
	
}
?>